import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Screen from '../views/Screen.vue'


Vue.use(VueRouter)

//嵌套不合理, 嵌套的 不应该用/开头
const routes = [
	{
		path: '/',
		redirect: '/login'
		// component: () => import('../views/Login.vue')
	},
	{
		path: '/home',
		name: 'Home',
		component: Home,
		children: [{
			// path: '/screen/:id',
			path: '/screen',
			name: 'Screen',
			component: Screen,
			props: true
			// component: () => import('../views/Screen.vue')
		},
		{
			path: '/group',
			name: 'Group',
			component: () => import('../views/Group.vue')
		},
		{
			path: '/status',
			name: 'Status',
			component: () => import('../views/Status.vue')
		},
		{
			path: '/resource',
			name: 'Resource',
			component: () => import('../views/Resource.vue')
		},
		{
			// children 应该不要 加/  加/  表示访问的是  绝对路径
			path: '/resource_upload',
			name: 'Resource_upload',
			component: () => import('../views/Resource_upload.vue')
		},
		{
			path: '/resource_text',
			name: 'Resource_text',
			component: () => import('../views/Resource_text.vue')
		},
		{
			path: '/schedule_edit',
			name: 'Schedule_edit',
			component: () => import('../views/Schedule_edit.vue')
		},
		{
			path: '/schedule_list',
			name: 'Schedule_list',
			component: () => import('../views/Schedule_list.vue')
		},
		{
			path: '/log_list',
			name: 'Log_list',
			component: () => import('../views/Log_list.vue')
		},
		{
			path: '/user_list',
			name: 'User_list',
			component:()=> import('../views/User_list.vue')
		}
		]
	},

	{
		path: '/about',
		name: 'About',
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () => import( /* webpackChunkName: "about" */ '../views/About.vue')
	},
	{
		path: '/login',
		name: 'Login',
		component: () => import('../views/Login.vue')
	}
]

const router = new VueRouter({
	mode: 'hash', ////默认是hash 还是 history?
	base: process.env.BASE_URL, ///????
	routes
})

router.beforeEach((to, from, next) => {
	console.log(to.path);
	if (to.path === '/login') {
		next()
	} else {
		let token = window.sessionStorage.getItem('Authorization')
		if (token ===null){
			// next('/login')
			next({
				path:'/login',
				query:{ redirect: to.fullPath}
			})
		}else{
			next()
		}
	}
})
export default router
